<?php
declare (strict_types = 1);

namespace app\validate\common\system;

use think\Validate;
use app\model\communal\FileModel;

class FileValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id' => ['require', 'number', 'gt:0'],
        'explain' => ['require', 'max:50'],
        'path' => ['require', 'max:255'],

        'orderBy' => ['checkOrderBy'],
        'orderDir' => ['in:asc,desc'],
        'page' => ['number', 'gt:0'],
        'page_size' => ['number', 'gt:0'],

        /*
         * amis批量快速编辑
         */
        'rowsDiff' => ['require', 'array'],
    ];

    protected $field = [
        'explain' => '解释说明',
        'path' => '存储路径',

        'page' => '当前页数',
        'page_size' => '每页条数',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
    ];

    /**
     * 场景
     * @var array
     */
    protected $scene = [
        'index' => ['page', 'page_size', 'start_time', 'end_time', 'orderBy', 'orderDir'],
        'options' => ['orderBy', 'orderDir'],
        'batchUpdate' => ['rowsDiff'],
        'look' => ['id'],
    ];

    /**
     * scene save
     *
     * @return $this
     */
    public function sceneSave()
    {
        return $this->only(FileModel::$saveColumn);
    }

    /**
     * scene update
     *
     * @return $this
     */
    public function sceneUpdate()
    {
        return $this->only(FileModel::$updateColumn);
    }

    /**
     * scene update
     *
     * @return $this
     */
    public function sceneItem()
    {
        return $this->only(['id', 'title'])
            ->remove('title', 'require')
            ->append('title', 'checkTitleUnique:update');
    }

    /**
     * 排序字段检测
     *
     * @param $value
     * @return bool|mixed
     */
    protected function checkOrderBy($value)
    {
        if (!in_array($value, FileModel::$column)) {
            return lang(':attribute not conform to the rules');
        }

        return true;
    }
}
